<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>

<%
    String basePath = request.getContextPath() + "/";
	List<String> list = (List<String>)request.getAttribute("list");
	for(String s : list){
		out.println(s);
	}
%>

<br>
<button id="updateBtn">更新</button><br>
<button id="clearBtn">清空</button><br>
<button id="jsonBtn">JSON</button><br>

<script type="text/javascript" src="<%= basePath%>js/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
	function post(config){
		var servlet = config.servlet;
		var operate = config.operate;
		
		var params = {};
		params.operate = operate;
		
		var keyword = ['servlet','operate','succ','fail','msg'];
		for(var key in config){
		    if(keyword.indexOf(key) < 0){
				params[key] = config[key];
			}
		}
		
		$.post(path(servlet), params, function(data){
			if('msg' in config){
				config.msg(data);
			}else {
				if(data.success == "true"){
					if('succ' in config){
						config.succ();
					}
				}else{
					if('fail' in config){
						config.fail();
					}
				}
			}
		}, "json");
	}
	
	function path(uri){
		return "<%= request.getContextPath() %>" + "/" + uri;
	}
	
	$(document).ready(function(){
		$("#updateBtn").click(function(){
			post({
			servlet:"DemoServlet",
			operate:"updateInfo",
			data:"qqqq",
			succ:function(){
				alert("更新成功");
				location.reload();
			}});
		});
		$("#clearBtn").click(function(){
			post({
			servlet:"DemoServlet",
			operate:"clearInfo",
			succ:function(){
				alert("清除成功");
				location.reload();
			}});
		});
		$("#jsonBtn").click(function(){
			post({
			servlet:"DemoServlet",
			operate:"jsonInfo",
			msg:function(data){
				alert(data);
			}});
		});
		
	});
	
</script>